﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using QLVMBAYDatacess;
using QLVMBAYDomain;

namespace QLVMBAY.Admin
{
    public partial class QLLoaiGhe : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGrid();
            }
        }
        private void LoadGrid()
        {
            grdLoaiGhe.DataSource = LoaiGheDA.Inst.GetAll(false);
            grdLoaiGhe.DataBind();
            if (grdLoaiGhe.PageCount <= 1)
            {
                grdLoaiGhe.PagerStyle.Visible = false;
            }
        }
        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            pnView.Visible = false;
            txtMaLG.Text = "";
            txtTenLG.Text = "";
            Insert = true;
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                LoaiGhe LG = new LoaiGhe();
                LG.MaLoaiGhe  = txtMaLG.Text.ToString();
                LG.TenLoaiGhe= txtTenLG.Text.ToString();
                
                if (Insert == true)
                {
                    LoaiGheDA.Inst.Insert(LG);
                }
                else
                {
                    LoaiGheDA.Inst.Update(LG);
                }
                LoadGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
        }

        protected void grdLoaiGhe_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            var itemLG = LoaiGheDA.Inst.GetByPrimaryKey(false, e.Item.Cells[0].Text);
            switch (e.CommandName)
            {
                case "Edit":
                    Insert = false;
                    txtMaLG.Text = itemLG.MaLoaiGhe.ToString();
                    txtMaLG.ReadOnly = true;
                    txtTenLG.Text = itemLG.TenLoaiGhe.ToString();
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;

                case "Delete":
                    LoaiGheDA.Inst.Delete(itemLG);
                    LoadGrid();
                    break;
            }
        }

        protected void grdLoaiGhe_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

        }

        protected void grdLoaiGhe_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdLoaiGhe.CurrentPageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}